<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*测试接口*/
class ApiTest extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	
	/*会员卡列表*/
	function index(){
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}	
	
	/*设置免指纹卡*/
	function add(){
		if(request()->isPost()){	
			$data=input('');
			if(!empty($data['card_no'])){
				$data['card_no1']=str_pad($data['card_no'], 10,'0',STR_PAD_LEFT);
				$data['card_no']=str_pad($data['card_no'], 10,'0',STR_PAD_LEFT);
			}
			$data['card_no']=$this->to_hex_revers($data['card_no']);
			//新增卡号
			$flag=$this->validate($data,'card');
			if($flag!==true){//参数验证错误
				return $this->ajax_error($flag,"0003",FALSE);
			}
			//新增会员
			$flag=$this->validate($data,'InterfaceApi.add');
			if($flag!==TRUE){
				return $this->ajax_error($flag,'0003',FALSE);
			}else{
				model('Card')->create($data,TRUE);
				model('InterfaceApi')->create($data,TRUE);
				$card_no1=$data['card_no1'];
				model('card')->where("card_no1='$card_no1'")->setField("fingerprint",str_replace("-", "", input("fingerprint",'')));
				return $this->ajax_success();
			}
		}else{
			echo $this->fetch();
		}
	}
	
	function edit(){
		if(empty(input('id'))){
			
	 	 $card_no=input('card_no/s');
		 
		 if(!empty($card_no)){
			$card_no=str_pad($card_no, 10,'0',STR_PAD_LEFT);
		}
		 
		 //卡表需要的数据
		 $card['id']=db('Card')->where("card_no1='$card_no'")->fetchsql(FALSE)->value("id");
		 if(!empty(input("type"))){
		 	 $card['type']=input("type");
		 }
		 if(!empty(input("owner_type"))){
		 	 $card['owner_type']=input("owner_type");
		 }
		 if(!empty(input("fingerprint"))){		 	 		 	 
		 	 $card['fingerprint']=str_replace("-", "", input("fingerprint",''));
		 }
		//////////////////////////////////////////////////////////////////////// 
	 	 //会员接口表需要修改的字段
	 	 $data['id']=db('interface')->where("card_no1='$card_no'")->fetchsql(FALSE)->value("id");
		 if(empty($data['id'])){
		 	return $this->ajax_error('','0004',config('err_code.0004'));
		 }
		 $flag=TRUE;
		 //需要修改字段的值
		 if(!empty(input('effect_date'))){
		 	$flag=$this->validate(input(''),'InterfaceApi.effect_date');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['effect_date']=input('effect_date');
		 }
		 
		 if(input('surplus')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.surplus');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['surplus']=input('surplus');
		 }
		 
		 if(input('in_varifycation')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_varifycation');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_varifycation']=input('in_varifycation');
		 }
		 
		  if(input('out_varifycation')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.out_varifycation');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			
		  if(!empty(input('mobile'))){
		  	$data['mobile']=input('mobile');
		  }
			
		 if(!empty(input('name'))){
		  	$data['name']=input('name');
		  }	

			$data['out_varifycation']=input('out_varifycation');
		 }
		 
		 
		  if(input('in_surplus_limt')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_surplus_limt');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_surplus_limt']=input('in_surplus_limt');
		 }
		  
		 if(input('in_surplus_sub')!==null){
		 	$flag=$this->validate(input(''),'InterfaceApi.in_surplus_sub');
			if($flag!==TRUE){
				return $this->ajax_error('','0003',$flag);
			} 
			$data['in_surplus_sub']=input('in_surplus_sub');
		 }
		if(input('status')!==null){
			$data['status']=input('status');
		 }
		
		if(input('start_date')!==null){
			$data['start_date']=input('start_date');
		 }
		
		$card['card_no1']=$card_no;
		$card['card_no']=$this->to_hex_revers($card_no);
		db('card')->update($card);// 修改表
		
		$data['card_no1']=$card_no;
		$data['card_no']=$this->to_hex_revers($card_no);
	 	db('interface')->update($data);
		 
		 return  $this->ajax_success();
	 
		}else{
			$model=db("Interface")->find(input("id"));
			$card=db("Card")->where("card_no='".$model['card_no']."'")->find();
			$model['owner_type']=$card['owner_type'];
			$model['fingerprint']=$card['fingerprint'];
			$model['type']=$card['type'];
			$this->assign("model",$model);
			echo $this->fetch();
		}
	}
	
	//删除员工
	function delete(){
			$id=input("id");//interface_id
			db("Card")->where("card_no = (select card_no from m_interface where id=$id)")->fetchsql(FALSE)->delete();
			db("Interface")->where("id =$id")->delete();
			$this->ajax_success("成功");
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10051001");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'card_no1'=>array("field" =>"card_no1","operator" =>"="),
					 );
	    $field="id,card_no1,free_finger,free_finger_varify,in_varifycation,in_surplus_limt,is_temp,card_no";
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$total=db("interface")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("interface")->alias("a")->field($field)->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		$cards=db("Card")->column("owner_type","card_no");
		foreach ($list as $key => $value) {
			$list[$key]['owner_type']=$cards[$value['card_no']];
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	
	protected function to_hex_revers($value){
		  if(strlen($value)==10){//10位10进制
		      $dec=str_pad(dechex(doubleval($value)), 8,"0",STR_PAD_LEFT);
		      $bit1=substr($dec, 0,2);
			  	$bit2=substr($dec, 2,2);
					$bit3=substr($dec, 4,2);
					$bit4=substr($dec, 6,2);
					return  strtoupper($bit4.$bit3.$bit2.$bit1);
		  }else{
		  	$this->ajax_error($data="",$code="0000",$msg="卡号不是10位的10进制数");
		  }
	}
	/*
	 * 安科系统卡号转换
	 * 16进制转10进制
	 * */
	protected function to_dec_revers($value){
		  	  $bit1=substr($value, 0,2);
			  	$bit2=substr($value, 2,2);
					$bit3=substr($value, 4,2);
					$bit4=substr($value, 6,2);
					$dec=hexdec($bit4.$bit3.$bit2.$bit1);		
				return	str_pad($dec, 10,'0',STR_PAD_LEFT);		
	 }
}
